package com.atguigu.aiproject.controller.user;

import com.atguigu.aiproject.dto.CreationQueryDTO;
import com.atguigu.aiproject.entity.Result;
import com.atguigu.aiproject.service.CreationLikeService;
import com.atguigu.aiproject.service.CreationService;

import com.atguigu.aiproject.service.CreationViewService;
import com.atguigu.aiproject.vo.CreationCoverVO;

import com.atguigu.aiproject.vo.CreationDetailsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;
@RestController
@CrossOrigin
@RequestMapping("/creation")
public class CreationSquareController {
    private final CreationService creationService;
    private final CreationLikeService creationLikeService;
    private final CreationViewService creationViewService;// 创建浏览服务

    public CreationSquareController(CreationService creationService, CreationLikeService creationLikeService, CreationViewService creationViewService) {
        this.creationService = creationService;
        this.creationLikeService = creationLikeService;
        this.creationViewService = creationViewService;
    }

    @GetMapping("/works")
    public Result<?> list(@RequestParam(defaultValue = "1") int pageNum,
                          @RequestParam(defaultValue = "10") int pageSize,
                          @ModelAttribute CreationQueryDTO queryDTO
                          ) {
        IPage<CreationCoverVO> resultPage = creationService.getCreationCoverVOList(pageNum, pageSize,queryDTO);
        return Result.success(resultPage);
    }
    @GetMapping("/details")
    public Result<?> details(@RequestParam int id) {
        CreationDetailsVO detail = creationService.getDetails(id);

        creationLikeService.setCurrentUserLikeStatus(detail, id);
        // 增加浏览量（去重）
        creationViewService.increaseViewCount(id);
        return Result.success(detail);
    }
    //展示各人的所有创作作品
    @GetMapping("/show")
    public Result<?> show(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam int id
    ) {
        return Result.success(creationService.getCreationCoverVOListByUserId(pageNum, pageSize, id));
    }
}
